<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="组织机构" prop="organstructure">
      <el-input v-model="dataForm.organstructure" placeholder="组织机构"></el-input>
    </el-form-item>
    <el-form-item label="参赛条件" prop="entryconditions">
      <el-input v-model="dataForm.entryconditions" placeholder="参赛条件"></el-input>
    </el-form-item>
    <el-form-item label="时间地址" prop="timeaddress">
      <el-input v-model="dataForm.timeaddress" placeholder="时间地址"></el-input>
    </el-form-item>
    <el-form-item label="活动安排" prop="activityarrangement">
      <el-input v-model="dataForm.activityarrangement" placeholder="活动安排"></el-input>
    </el-form-item>
    <el-form-item label="活动议程" prop="activityagenda">
      <el-input v-model="dataForm.activityagenda" placeholder="活动议程"></el-input>
    </el-form-item>
    <el-form-item label="活动背景" prop="activitybg">
      <el-input v-model="dataForm.activitybg" placeholder="活动背景"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          organstructure: '',
          entryconditions: '',
          timeaddress: '',
          activityarrangement: '',
          activityagenda: '',
          activitybg: ''
        },
        dataRule: {
          organstructure: [
            { required: true, message: '组织机构不能为空', trigger: 'blur' }
          ],
          entryconditions: [
            { required: true, message: '参赛条件不能为空', trigger: 'blur' }
          ],
          timeaddress: [
            { required: true, message: '时间地址不能为空', trigger: 'blur' }
          ],
          activityarrangement: [
            { required: true, message: '活动安排不能为空', trigger: 'blur' }
          ],
          activityagenda: [
            { required: true, message: '活动议程不能为空', trigger: 'blur' }
          ],
          activitybg: [
            { required: true, message: '活动背景不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/eventsweb/casesharing/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.organstructure = data.casesharing.organstructure
                this.dataForm.entryconditions = data.casesharing.entryconditions
                this.dataForm.timeaddress = data.casesharing.timeaddress
                this.dataForm.activityarrangement = data.casesharing.activityarrangement
                this.dataForm.activityagenda = data.casesharing.activityagenda
                this.dataForm.activitybg = data.casesharing.activitybg
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/eventsweb/casesharing/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'organstructure': this.dataForm.organstructure,
                'entryconditions': this.dataForm.entryconditions,
                'timeaddress': this.dataForm.timeaddress,
                'activityarrangement': this.dataForm.activityarrangement,
                'activityagenda': this.dataForm.activityagenda,
                'activitybg': this.dataForm.activitybg
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
